package com.github.microwww.upms.service;

import com.github.microwww.upms.domain.UserRole;
import com.github.microwww.upms.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import java.util.Optional;

@Service()
@Transactional()
public class UserRoleService {

    @Autowired()
    private UserRoleRepository userRoleRepository;

    public UserRoleRepository getUserRoleRepository() {
        return userRoleRepository;
    }

    public void setUserRoleRepository(UserRoleRepository userRoleRepository) {
        this.userRoleRepository = userRoleRepository;
    }

    public Page<UserRole> findAll(int page, int size) {
        return this.userRoleRepository.findAll(PageRequest.of(page, size));
    }

    public Optional<UserRole> findById(int id) {
        return this.userRoleRepository.findById(id);
    }

    public UserRole getOrElseThrow(int id) {
        return this.userRoleRepository.findById(id).orElseThrow(() -> {
            return new com.github.microwww.upms.exception.ExistException.NotExist(UserRole.class);
        });
    }
}
